import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;


public class ActualReporter {
	
	ArrayList<String> words;
	int max;
	long startTime;
	
	public ActualReporter(ArrayList<String> keyWords, int maxPages) {
		words = keyWords;
		max = maxPages;
		startTime = System.currentTimeMillis();
	}

	public void report(String url, int pageCount, int totalWords, int totalURLs, int[] wordCount, long runningTime) {
		Writer output = null;
		File f = new File("reporterData.txt");
		
		try {
            output = new BufferedWriter(new FileWriter(f, true));
            
    		System.out.println("Parsed: " + url);
    		output.append("Parsed: " + url + "\n");
    		System.out.println("PagesRetrieved: " + pageCount);
    		output.append("PagesRetrieved: " + pageCount + "\n");
    		System.out.println("Average Words Per Page: " + totalWords/pageCount);
    		output.append("Average Words Per Page: " + totalWords/pageCount + "\n");
    		System.out.println("Average URLs Per Page: " + totalURLs/pageCount);
    		output.append("Average URLs Per Page: " + totalURLs/pageCount + "\n");
    		System.out.println("Keyword \t Avg. hits per page \t Total hits");
    		output.append("Keyword \t\t Avg. hits per page \t Total hits" + "\n");
    		int i = 0;
    		for(String s : words) {
    			String t = String.format("%11s\t\t %.3f\t\t\t%d\n", s, wordCount[i]/(pageCount*1.0), wordCount[i]);
    			System.out.print(t);
    			output.append(t);
    			i++;
    		}
    		
    		System.out.println("Page limit: " + max);
    		output.append("Page limit: " + max + "\n");
    		System.out.println("Average parse time per page: " + runningTime/pageCount/1000.0 + " sec");
    		output.append("Average parse time per page: " + runningTime/pageCount/1000.0 + " sec\n");
    		System.out.println("Total parse time: " + runningTime/1000.0 + " sec");
    		output.append("Total parse time: " + runningTime/1000.0 + " sec");
    		System.out.println("Total running time: " + (System.currentTimeMillis() - startTime)/1000 + " sec\n");
    		output.append("Total running time: " + (System.currentTimeMillis() - startTime)/1000 + " sec\n\n");
    		
            output.close();
    		
		} catch (IOException e) {
            e.printStackTrace();
		}		
		if (pageCount == max) System.exit(0);
	}
}
